package com.base.lib.base.view

import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.flyco.tablayout.CommonTabLayout
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.base.lib.R
import com.base.lib.base.model.TabEntity
import com.base.lib.base.utils.DensityUtil
import com.base.lib.base.utils.StatusBarUtils

/**
 * Describe:
 * 基于 MVVM PAGE页面
 *
 */
abstract class BaseMvvmPageActivity : BaseActivity(){


    protected var mPageTopBar: RelativeLayout? = null
    protected var ivWriteBack: ImageView? = null
    protected var ivSearch: ImageView? = null
    protected var viewPager: ViewPager2? = null
    protected var tabLayout: CommonTabLayout? = null

    protected var pageChangeCallback: PageChangeCallback? = null
    protected val adapter: VpAdapter by lazy { VpAdapter(mContext).apply { addFragments(createFragments) } }
    protected var offscreenPageLimit = 1
    abstract val createTitles: ArrayList<TabEntity>
    abstract val createFragments: Array<Fragment>

    override fun onBindLayout(): Int = R.layout.base_page_fragment

    override fun initCommonView() {
        super.initCommonView()
        setupViews()
    }

    open fun setupViews() {
        mPageTopBar = mContentView.findViewById(R.id.pageTopBar);
        ivWriteBack = mContentView.findViewById(R.id.ivWriteBack)
        ivSearch = mContentView.findViewById(R.id.ivSearch)
        ivWriteBack?.setOnClickListener {
            finish();
        }
        if (!DensityUtil.isSmallWindow(this)) {
            val layoutParams = mPageTopBar?.layoutParams
            layoutParams?.height = StatusBarUtils.getStatusBarHeight(this) + DensityUtil.dip2px(this, 46)
            mPageTopBar?.layoutParams = layoutParams
        }
//        setStatusBarBackground(R.color.black)
        viewPager = mContentView.findViewById(R.id.viewPager)
        tabLayout = mContentView.findViewById(R.id.tabLayout)
        initViewPager()
    }

    protected fun initViewPager() {
        if (createTitles != null && createTitles.size > 0){
            viewPager?.offscreenPageLimit = offscreenPageLimit
            viewPager?.adapter = adapter
            val arrayList = ArrayList<CustomTabEntity>()
            createTitles.forEach {
                arrayList.add(it)
            }
            tabLayout?.setTabData(arrayList)
            tabLayout?.setOnTabSelectListener(object : OnTabSelectListener {

                override fun onTabSelect(position: Int) {
                    viewPager?.currentItem = position
                }

                override fun onTabReselect(position: Int) {

                }
            })
            pageChangeCallback = PageChangeCallback()
            viewPager?.registerOnPageChangeCallback(pageChangeCallback!!)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        pageChangeCallback?.run { viewPager?.unregisterOnPageChangeCallback(this) }
        pageChangeCallback = null
    }

    inner class PageChangeCallback : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            tabLayout?.currentTab = position
        }
    }

    inner class VpAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
        private val fragments = mutableListOf<Fragment>()
        fun addFragments(fragment: Array<Fragment>) {
            fragments.addAll(fragment)
        }
        override fun getItemCount() = fragments.size
        override fun createFragment(position: Int) = fragments[position]
    }
}
